/*
 * ItemPanel.java
 *
 * Created on Feb 17, 2012, 2:26:49 PM
 */
package dkgg2012group22imat.view;

import dkgg2012group22imat.model.IMatUtilities;
import dkgg2012group22imat.controller.CartProductController;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import org.jdesktop.application.ResourceMap;
import org.netbeans.lib.awtextra.AbsoluteConstraints;
import se.chalmers.ait.dat215.project.ShoppingItem;

/**
 *
 * @author jonas
 */
public class CartProductPanel extends javax.swing.JPanel {

    CartProductController controller;
    boolean inside;
    Dimension small = new Dimension(150, 150);
    Dimension big = new Dimension(200, 200);
    ShoppingItem si;

    /** Creates new form ItemPanel */
    public CartProductPanel(ShoppingItem si) {
        initComponents();
        controller = new CartProductController(si, this);
        this.si = si;
        this.setAmount(si.getAmount());

        IMatUtilities.addHoverListener(this, new MouseAdapter() {

            @Override
            public void mouseExited(MouseEvent evt) {
                formMouseExited(evt);
            }

            @Override
            public void mouseEntered(MouseEvent evt) {
                formMouseEntered(evt);
            }
        });

        remove(priceLabel);
        add(priceLabel, new AbsoluteConstraints(new Point(153 + IMatUtilities.getStringDimension(unitLabel.getFont(), unitLabel.getText()).width, 32), null));

    }

    @Override
    public void paintComponent(Graphics g) {
        ResourceMap resourceMap = DKGG2012Group22iMatApp.getApplication().getContext().getResourceMap(CartProductPanel.class);

        IMatUtilities.drawTiled(g, this.getSize(), resourceMap.getImageIcon("JPanel.background"));

        g.setColor(new Color(204, 204, 204));
        g.drawRect(-1, -1, this.getWidth() + 1, this.getHeight());//top and bottom border
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        priceLabel1 = new dkgg2012group22imat.view.PriceLabel();
        productNameLabel = new javax.swing.JLabel();
        quantitySpinner = new javax.swing.JSpinner();
        deleteButton = new javax.swing.JButton();
        favoriteToggleButton = new javax.swing.JButton();
        productImageLabel = new dkgg2012group22imat.view.ProductImageLabel();
        unitLabel = new javax.swing.JLabel();
        priceLabel = new dkgg2012group22imat.view.PriceLabel();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(dkgg2012group22imat.view.DKGG2012Group22iMatApp.class).getContext().getResourceMap(CartProductPanel.class);
        priceLabel1.setText(resourceMap.getString("priceLabel1.text")); // NOI18N
        priceLabel1.setName("priceLabel1"); // NOI18N

        setBackground(resourceMap.getColor("Form.background")); // NOI18N
        setMaximumSize(new java.awt.Dimension(2147483647, 85));
        setMinimumSize(new java.awt.Dimension(160, 85));
        setName("Form"); // NOI18N
        setPreferredSize(new java.awt.Dimension(270, 85));
        setRequestFocusEnabled(false);
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        productNameLabel.setFont(resourceMap.getFont("productNameLabel.font")); // NOI18N
        productNameLabel.setText(resourceMap.getString("productNameLabel.text")); // NOI18N
        productNameLabel.setName("productNameLabel"); // NOI18N
        add(productNameLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 6, 170, -1));

        quantitySpinner.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(1), Integer.valueOf(1), null, Integer.valueOf(1)));
        quantitySpinner.setMinimumSize(new java.awt.Dimension(37, 26));
        quantitySpinner.setName("quantitySpinner"); // NOI18N
        quantitySpinner.setPreferredSize(new java.awt.Dimension(55, 27));
        quantitySpinner.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                quantitySpinnerMouseEntered(evt);
            }
        });
        quantitySpinner.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                quantitySpinnerStateChanged(evt);
            }
        });
        add(quantitySpinner, new org.netbeans.lib.awtextra.AbsoluteConstraints(87, 30, -1, 25));
        quantitySpinner.setUI(new StyledSpinnerUI());

        deleteButton.setIcon(resourceMap.getIcon("deleteButton.icon")); // NOI18N
        deleteButton.setText(resourceMap.getString("deleteButton.text")); // NOI18N
        deleteButton.setToolTipText(resourceMap.getString("deleteButton.toolTipText")); // NOI18N
        deleteButton.setBorderPainted(false);
        deleteButton.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        deleteButton.setName("deleteButton"); // NOI18N
        deleteButton.setPreferredSize(new java.awt.Dimension(80, 24));
        deleteButton.setRolloverIcon(resourceMap.getIcon("deleteButton.rolloverIcon")); // NOI18N
        deleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButtonActionPerformed(evt);
            }
        });
        add(deleteButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(190, 60, -1, -1));

        favoriteToggleButton.setIcon(resourceMap.getIcon("favoriteToggleButton.icon")); // NOI18N
        favoriteToggleButton.setText(resourceMap.getString("favoriteToggleButton.text")); // NOI18N
        favoriteToggleButton.setToolTipText(resourceMap.getString("favoriteToggleButton.toolTipText")); // NOI18N
        favoriteToggleButton.setBorderPainted(false);
        favoriteToggleButton.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        favoriteToggleButton.setDisabledIcon(resourceMap.getIcon("favoriteToggleButton.disabledIcon")); // NOI18N
        favoriteToggleButton.setDisabledSelectedIcon(resourceMap.getIcon("favoriteToggleButton.disabledSelectedIcon")); // NOI18N
        favoriteToggleButton.setName("favoriteToggleButton"); // NOI18N
        favoriteToggleButton.setPreferredSize(new java.awt.Dimension(70, 22));
        favoriteToggleButton.setPressedIcon(resourceMap.getIcon("favoriteToggleButton.pressedIcon")); // NOI18N
        favoriteToggleButton.setRolloverIcon(resourceMap.getIcon("favoriteToggleButton.rolloverIcon")); // NOI18N
        favoriteToggleButton.setRolloverSelectedIcon(resourceMap.getIcon("favoriteToggleButton.rolloverSelectedIcon")); // NOI18N
        favoriteToggleButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                favoriteToggleButtonActionPerformed(evt);
            }
        });
        add(favoriteToggleButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 60, -1, -1));

        productImageLabel.setBorder(javax.swing.BorderFactory.createLineBorder(resourceMap.getColor("productImageLabel.border.lineColor"))); // NOI18N
        productImageLabel.setText(resourceMap.getString("productImageLabel.text")); // NOI18N
        productImageLabel.setMaximumSize(new java.awt.Dimension(70, 53));
        productImageLabel.setMinimumSize(new java.awt.Dimension(70, 53));
        productImageLabel.setName("productImageLabel"); // NOI18N
        productImageLabel.setPreferredSize(new java.awt.Dimension(70, 53));
        productImageLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                productImageLabelMouseClicked(evt);
            }
        });
        add(productImageLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 17, 70, 53));

        unitLabel.setFont(resourceMap.getFont("unitLabel.font")); // NOI18N
        unitLabel.setText(resourceMap.getString("unitLabel.text")); // NOI18N
        unitLabel.setName("unitLabel"); // NOI18N
        add(unitLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(148, 33, -1, -1));

        priceLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        priceLabel.setText(resourceMap.getString("priceLabel.text")); // NOI18N
        priceLabel.setFont(resourceMap.getFont("priceLabel.font")); // NOI18N
        priceLabel.setName("priceLabel"); // NOI18N
        add(priceLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 32, -1, -1));
    }// </editor-fold>//GEN-END:initComponents

private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed
    controller.delete();
}//GEN-LAST:event_deleteButtonActionPerformed

private void quantitySpinnerStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_quantitySpinnerStateChanged
    controller.spinnerChanged(this.getAmount());
}//GEN-LAST:event_quantitySpinnerStateChanged

private void quantitySpinnerMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_quantitySpinnerMouseEntered
    controller.hooverd();
}//GEN-LAST:event_quantitySpinnerMouseEntered

private void favoriteToggleButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_favoriteToggleButtonActionPerformed
    controller.favorize();
}//GEN-LAST:event_favoriteToggleButtonActionPerformed

private void productImageLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_productImageLabelMouseClicked
    if (evt.getSource() instanceof ProductImageLabel) {
        ProductImageLabel pil = (ProductImageLabel) evt.getSource();
        System.out.println(pil.getLocationOnScreen());
    }
}//GEN-LAST:event_productImageLabelMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    public javax.swing.JButton deleteButton;
    public javax.swing.JButton favoriteToggleButton;
    public dkgg2012group22imat.view.PriceLabel priceLabel;
    private dkgg2012group22imat.view.PriceLabel priceLabel1;
    public dkgg2012group22imat.view.ProductImageLabel productImageLabel;
    public javax.swing.JLabel productNameLabel;
    public javax.swing.JSpinner quantitySpinner;
    public javax.swing.JLabel unitLabel;
    // End of variables declaration//GEN-END:variables

    public ShoppingItem getShoppingItem() {
        return si;
    }

    public int getAmount() {
        return Integer.parseInt(quantitySpinner.getValue().toString());
    }

    public void setAmount(double amount) {
        quantitySpinner.setValue(new Integer((int) amount));
    }

    private void formMouseExited(java.awt.event.MouseEvent evt) {
        controller.dehoover();
    }

    private void formMouseEntered(java.awt.event.MouseEvent evt) {
        controller.hooverd();
    }
    public CartProductController getController(){
        return controller;
    }
}
